
package stcu.control;

import stcu.*;
import stcu.forms.*;
import stcu.robjects.*;
import com.nvlled.route.*;
import javax.swing.SwingUtilities;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

import java.util.*;
import java.text.SimpleDateFormat;

public class ControlledServerForm extends ServerForm
    implements Group.Listener {

    private Server server;
    private Group group;
    private DefaultListModel<Account> userListModel;

    public ControlledServerForm(Server server, Group group) {
        userListModel = new DefaultListModel<Account>();
        userListArea.setModel(userListModel);
        this.server = server;
        this.group = group;

        addListeners();
        group.listMembers();
    }


    private void addListeners() {
        sendButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                String message = messageBox.getText();
                messageBox.setText("");

                group.send(message);
            }
        });
        group.addListener(this);
    }

    public void messageReceived(GroupMessage message) {
        Calendar cal = Calendar.getInstance();
		cal.getTime();
		SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss aa");
		
		
		String line = message.getSender() + ": " +
            message.getMessage();
		messageLog.append(line+"\n\n");
		messageLog.append("\t"+"\t"+"\t"+"               "+"Received "+sdf.format(cal.getTime())+"\n");
    }

    public void memberJoined(MemberAdd member) {
        String name = member.getName();
        if (member.isNewMember()) {
            String line = name + " has joined the group";
            messageLog.append(line+"\n\n");
        }
        if (! userListModel.contains(name)) {
            userListModel.addElement(member.getAccount());
        }
    }

    public void memberLeft(MemberDel member) {
        System.out.println("%%");
        String line = member.getName() + " has left the group";
        messageLog.append(line+"\n\n");
        userListModel.removeElement(member.getAccount());
    }

}
